﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace PlantsZombies.Common
{
    public class ImageObject : IDisposable
    {
        public ImageObject(string path) : this(new Bitmap(path))
        {
        }
        public ImageObject(Bitmap bitmap)
        {
            SetBitmap(bitmap);
            
        }
        private void SetBitmap(Bitmap bitmap)
        {
            var bit = bitmap;// CompressImage(bitmap, bitmap.RawFormat == ImageFormat.Png ? 50 : 100);
            Width = bit.Width;
            Height = bit.Height;
            BitmapPtr = bit.GetHbitmap();
            bit.Dispose();
        }
        public int Width { get; private set; }
        public int Height { get; private set; }
        public IntPtr BitmapPtr { get; private set; }

        public static Bitmap CompressImage(Bitmap bmp, int quality)
        {
            ImageCodecInfo codecInfo = GetEncoder(bmp.RawFormat); //图片编解码信息
            System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;
            EncoderParameters encoderParameters = new EncoderParameters(1);
            EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
            encoderParameters.Param[0] = encoderParameter; //编码器参数
            using (MemoryStream memoryStream = new MemoryStream())
            {
                bmp.Save(memoryStream, codecInfo, encoderParameters); // 保存压缩图到MemoryStream
                memoryStream.Position = 0; // 重置流的位置，以便从流中读取

                // 从MemoryStream中创建新的Bitmap对象
                Bitmap compressedBmp = new Bitmap(memoryStream);
                memoryStream.Close();
                encoderParameters.Dispose();
                return compressedBmp; // 返回压缩后的Bitmap
            }
        }

        private static ImageCodecInfo GetEncoder(ImageFormat rawFormat)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == rawFormat.Guid)
                {
                    return codec;
                }
            }
            return null;
        }


        private bool _disposed;
        private object _obj = new object();
        public void Dispose()
        {
            Dispose(true);
            // 请求公共语言运行时不要调用该对象上的终结器（在C#中就是指不要调用析构函数）
            GC.SuppressFinalize(this);
        }
        [System.Runtime.InteropServices.DllImport("Kernel32")]
        private extern static Boolean CloseHandle(IntPtr handle);
        protected virtual void Dispose(bool disposing)
        {
            // 保证线程安全
            lock (_obj)
            {
                if (!_disposed)
                {
                    if (disposing)
                    {
                        // Release managed resources
                        //if (Bitmap != null)
                        //{
                        //    Bitmap.Dispose();
                        //    Bitmap = null;
                        //}
                    }

                    // Release unmanaged resources
                    if (BitmapPtr != IntPtr.Zero)
                    {
                        GDI32.DeleteObject(BitmapPtr);
                        BitmapPtr = IntPtr.Zero;
                    }
                    _disposed = true;
                }
            }
        }
        public void Close()
        {
            Dispose();
        }
        ~ImageObject()
        {
            Dispose(false);
        }
    }
}
